import {
  Column,
  Entity,
  JoinColumn,
  ManyToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { User } from './User';

@Entity('logs', { schema: 'test' })
export class Logs {
  constructor(
    id: number,
    date: Date,
    ip: string,
    method: string,
    url: string,
    statusCode: number,
    responseTime: number,
    user: User,
  ) {
    this.id = id;
    this.date = date;
    this.ip = ip;
    this.method = method;
    this.url = url;
    this.statusCode = statusCode;
    this.responseTime = responseTime;
    this.user = user || null;
  }
  @PrimaryGeneratedColumn({ type: 'int', name: 'id' })
  id: number;

  @Column('datetime', { name: 'date' })
  date: Date;

  @Column('varchar', { name: 'ip', length: 255 })
  ip: string;

  @Column('varchar', { name: 'method', length: 255 })
  method: string;

  @Column('varchar', { name: 'url', length: 255 })
  url: string;

  @Column('int', { name: 'statusCode' })
  statusCode: number;

  @Column('int', { name: 'responseTime' })
  responseTime: number;

  @ManyToOne(() => User, (user) => user.logs, {
    onDelete: 'NO ACTION',
    onUpdate: 'NO ACTION',
  })
  @JoinColumn([{ name: 'userId', referencedColumnName: 'id' }])
  user: User;
}
